/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    Input, TextArea, Text, useTheme, BlobInputList, useToast, Button,
} from "customize-easy-ui-component";
import {InspectRecordLayout, InternalItemProps, useItemInputControl} from "../../common/base";
import {setupItemAreaRoute} from "./orcIspConfig";
import {EditStorageContext} from "../../StorageContext";
import {useMedia} from "use-media";
import {assertNamesUnique} from "../../common/eHelper";
import {EachMeasureCritConfig} from "../../common/msCriteria";
import {itemA复检} from "../editor";
import {render层站门数} from "../../common/render";
import {楼盘地点性质} from "../../elevator/RoomLessJJ/orcBase";
import {itemA结论} from "./Conclusion";
import {itemA间隙} from "./DoorGap";
import {itemA动速校} from "./SpeedLimiter";
import {itemA运行质} from "./OperationQuality";
import {itemn平衡} from "./Equilibrium";

//唯一性检查：也会主动纳入names[]的字段名。
const 发生选=['有','没有'];
const 状况选=['正常','良好','较差'];
export const input使用情况={
    edit:(inp:any,setInp:(a:any)=>void, orc:any)=>{
        return <div css={{display: 'flex',flexWrap: 'wrap',alignItems: 'center'}}>
            <div css={{display: 'inline-flex',alignItems: 'center'}}>
            该电梯投入使用该电梯
                <Input value={inp?.限超过 ||''} size={2} style={{display: 'inline-flex', width: 'unset'}}
                   onChange={e => setInp({ ...inp, 限超过: e.currentTarget.value||undefined }) }/>
            年，
            </div>
            期间电梯
            <div css={{display: 'inline-flex',alignItems: 'center'}}>
                <BlobInputList value={inp?.发生过 ||''} datalist={发生选} rows={1}
                           style={{display: 'inline-flex', width: '4rem', maxWidth: '100%',}}
                           onListChange={v => setInp({ ...inp, 发生过: v||undefined })}/>
            发生过事故或重大故障，</div>
            <div css={{display: 'inline-flex',alignItems: 'center'}}>
                <BlobInputList value={inp?.被重大 ||''} datalist={发生选} rows={1}
                           style={{display: 'inline-flex', width: '4rem', maxWidth: '100%',}}
                           onListChange={v => setInp({ ...inp, 被重大: v||undefined })}/>
            被重大修理或改造过。</div>
            <div css={{display: 'inline-flex',alignItems: 'center'}}>
            电梯运行环境
                <BlobInputList value={inp?.行环境 ||''} datalist={状况选} rows={1}
                           style={{display: 'inline-flex', width: '4rem', maxWidth: '100%',}}
                           onListChange={v => setInp({ ...inp, 行环境: v||undefined })}/>
            </div>
            <div css={{display: 'inline-flex',alignItems: 'center'}}>
            ，电梯运行状况
                <BlobInputList value={inp?.行状况 ||''} datalist={状况选} rows={1}
                           style={{display: 'inline-flex', width: '4rem', maxWidth: '100%',}}
                           onListChange={v => setInp({ ...inp, 行状况: v||undefined })}/>
            。
            </div>
        </div>;
    },
    names: ['限超过','发生过','被重大','行环境','行状况' ],
    toTail: '设备使用情况',
    view:(orc:any)=>{
        return <div css={{display: 'flex',flexDirection: 'column',alignItems: 'flex-start'}}>
            该电梯投入使用该电梯 {orc?.限超过} 年，期间电梯 {orc?.发生过} 发生过事故或重大故障，{orc?.被重大}&nbsp;
            被重大修理或改造过 。电梯运行环境 {orc?.行环境}，电梯运行状况 {orc?.行状况} 。
        </div>
    }
};
const 制动型选=['柱塞式杠杆鼓式制动器','直压鼓式制动器','钳盘式制动器','轴向盘式制动器'];
export const config设备概况 = [
    [['设备名称', '_$设备名称'], ['使用登记编号', '_$使用证号'], ],
    [['使用单位名称', '_$使用单位'], ['楼盘名称', '_$楼盘'],],
    [['单位地址', '_$使用单位地址'],  ],
    [['楼盘地址', '_$楼盘地址'] ],
    [['分支机构名称', '_$分支机构'], ['分支机构地址', '_$分支机构地址'] ],
    [['联系人','_$设备联系人'],['联系电话','_$设备联系手机'] ],
    [['安装地点','_$设备使用地点'], ['使用单位编号', '_$单位内部编号'], ],
    [['设备号','_$eqpcod'],['制造日期','_$制造日期']],
    [['产品型号', '_$型号'], ['产品编号', '_$出厂编号'] ],
    [['额定载重量','_$额定载荷','kg'], ['额定速度', '_$运行速度','m/s'] ],
    [['层站门数', '_$电梯层数',render层站门数], ['制动器型式', {n:'制器型',t:'l',l:制动型选},] ],
    [['制造单位名称', '_$制造单位'], ['维保单位名称', '_$维保单位'] ],
    [['使用场所', {n: '楼地性质', t: 'l', l: 楼盘地点性质}], ['上行超速保护装置型号', '_$上行保护型号'],],
    [['轿厢意外移动保护装置型号', '_$意外保护型号'], ['是否非金属反绳轮',{n:'非金反绳',t:'b'}] ],
    [['设备使用情况', '限超过', input使用情况] ],
];

export const config观测数据 = [
    [{n: '绳直径', t: ['11.1', <Text css={{fontSize:'0.875rem'}}>磨损后的钢丝绳直径不小于钢丝绳公称直径的90%。</Text>],
                      x: '钢丝绳直径', u: 'mm', c: '四', d: 1,},
      {n: '绳公称径', t: [undefined, undefined], x: '公称直径', c: '四', d: 1,},
    ],
    [{check: '11.1(1)'}],
    [{n: '断丝数', t: [undefined, '一个捻距内出现的断丝数'], x: '断丝数', u: '根', c: '四', d: 0,},
    ],
    [{check: '11.1(2)'}],
    [{n: '井壁距', t: ['14.6', '轿厢与面对轿厢入口的井道壁的间距'],
                           x: '局部高度>0.5m时，间距', u: 'm', c: '四', d: 1,},
      {n: '井壁距货', t: [undefined, undefined], x: '局部高度<0.5m时或者采用垂直滑动门的载货电梯，间距', c: '四', d: 1,},
    ],
    [{check: '14.6'}],
    [{n: '越程距允', t: ['15.3', <Text css={{fontSize:'0.875rem'}}>当轿厢位于顶层端站平层位置时，对重装置撞板与其缓冲器顶面间的距离不超过对重越程距离标识上标注的最大允许值
                      </Text>], x: '最大允许值', u: 'mm', c: '四', d: 1,},
      {n: '越程距', t: [undefined, null], x: '测量值', c: '四', d: 1,},
    ],
    [{check: '15.3'}],
] as EachMeasureCritConfig[][];

export const tail附注= <Text css={{"@media print": {fontSize: '0.75rem'}}}>
    注1：检测对象为液压电梯时，U10代替U1和U5纳入评分；检测对象为非液压电梯时，U10不纳入评分<br/>
    注2：项目编号后A表示必须整改项目、B表示现行安全技术规范或标准新要求的项目,无标注的表示建议整改项目，标有※的项目为遵循“水桶短板理论”的评估项目
</Text>;

/**按照编辑器 recordPrintList 列表的编排顺序来配设的：
 * */
export const EntranceSetup =
    React.forwardRef((
        {children, show, alone = true, repId, verId, rep}: InternalItemProps, ref
    ) => {
        const theme = useTheme();
        const atPrint = useMedia('print');
        const toast = useToast();
        const doCheckNames = React.useCallback((verId: string) => {
            const impressionismAs = setupItemAreaRoute({rep, noDefault: true, theme});
            const result = assertNamesUnique([{value: rep?.tzFields}, {value: impressionismAs?.Item, type: 'impr'},
                {value: config设备概况, type:'esnt'}, {value:[...itemA结论,  ...itemA复检] },
                {value: config观测数据, type:'mesB'},
                {value:[...itemA间隙, ...itemA动速校, ...itemA运行质, ...itemn平衡 ] },
                {value:['unq', '仪器表','检验条件','_Oitems'  ]} ]);
            toast({title: "完成！", subtitle: result ? "没发现冲突" : "测试开关没开", intent: "success"});
        }, [rep,toast,theme]);
        const {storage, setStorage} =React.useContext(EditStorageContext) as any;
        const getInpFilter = React.useCallback((par: any) => {
            const {_tblFixed, } =par||{};
            return {_tblFixed, };
        }, []);
        const {inp, setInp} = useItemInputControl({ref});
        if (atPrint) return null;
        else return (
            <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show}
                                 alone={alone} label={'初始化本报告，默认值配置等'}>
                {process.env.REACT_APP_TEST==='true' && <><div>
                    <Text variant="h5">构建开发模板时的工具：校验模板的存储name冲突；</Text>
                    <Button intent='primary' onPress={() => doCheckNames(verId!)}>校验模板name唯一性</Button>
                    <Text variant="h5">设置待测试表格的各列宽度：</Text>
                    <TextArea value={storage?._tblFixed || ''} rows={2} onChange={e =>{
                        JSON.parse(e.currentTarget.value??'[]');
                        setStorage({ ...storage, _tblFixed: e.currentTarget.value || undefined}); }}/>
                </div>
                </>
                }
                <hr/>
            </InspectRecordLayout>
    );
});
